package com.minisoft.pingan;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author JueSu
 * @description: TODO
 * @date 2023/12/10 12:23
 */
public class ExportBaoJia {
    public static void main(String[] args) {
        String outputFilePath = "D:\\temp/tuanchebaojia-"+ DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN) +".xlsx";
        List<String> columnList = ListUtil.toList("车牌号","车架号","发动机号","座位/吨位","初次登记日期","车辆种类");

        int fileCount = 35;

        List<List<Object>> rows = new ArrayList<>();
        for (int i = 1;i <= fileCount;i++) {
            Console.log("处理文件："+i+"/"+fileCount);
            // Read JSON data from the input file
            String jsonData = FileUtil.readString(ExportBaoJia.class.getResource("/tuanchebaojia/"+i),"utf-8");

            // Parse JSON data to get rows and column_list
            JSONObject jsonObject = JSON.parseObject(jsonData);
            JSONObject data = jsonObject.getJSONObject("quotationResultDTOJson");
            JSONObject vehicleTarget = data.getJSONArray("voucherDetailList").getJSONObject(0).getJSONObject("voucher").getJSONObject("vehicleTarget");

            List<Object> row = new ArrayList<>();
            row.add(vehicleTarget.get("vehicleLicenceCode"));
            row.add(vehicleTarget.get("vehicleFrameNo"));
            row.add(vehicleTarget.get("engineNo"));
            row.add(vehicleTarget.get("vehicleSeats") + "/" + vehicleTarget.getString("wholeWeight"));
            row.add(vehicleTarget.getString("firstRegisterDate").split(" ")[0]);
            row.add(vehicleTarget.get("vehicleTypeName"));

            rows.add(row);
        }
        // Create Excel writer
        ExcelWriter writer = ExcelUtil.getWriter(outputFilePath);

        // Set the header row
        writer.writeHeadRow(columnList);

        // Write data rows
        writer.write(rows);

        // Close the writer and save the Excel file
        writer.close();

        System.out.println("done");
    }
}
